package com.woniuxy.classteacher.service.impl;

import com.woniuxy.classteacher.mapper.*;
import com.woniuxy.classteacher.service.ClassScoreInfoService;
import com.woniuxy.commons.entity.AnswerSheet;
import com.woniuxy.commons.entity.StudentInfo;
import com.woniuxy.commons.entity.TestPaper;
import com.woniuxy.commons.entity.wyh.TeamInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 李英勇
 * @date 2020/7/29 - 16:46
 */
@Service
public class ClassScoreInfoServiceImpl implements ClassScoreInfoService {
    @Autowired
    private AnswerSheetMapper answerSheetMapper;
    @Autowired
    private RedisTemplate<String, Object> redisTemplate;
    @Autowired
    private ChangeClassMapper changeClassMapper;
    @Autowired
    private ClassMapper classMapper;
    @Autowired
    private StudentMapper studentMapper;
    @Autowired
    private TestPaperMapper testPaperMapper;

    /**
     * @Author: 李英勇
     * @Description: 按条件查询学生各科考试的成绩
     * @DateTime: 2020/8/5 16:27
     * @Params: [testPaper, refreshToken, clazz]
     * @Return java.util.List<com.woniuxy.commons.entity.AnswerSheet>
     */
    @Override
    public List<AnswerSheet> findScore(TestPaper testPaper, String refreshToken, String clazz) {
        //在redis中获取用户的role和rid
        //通过refreshToken获取数据
        Map<Object, Object> data = redisTemplate.opsForHash().entries(refreshToken);
        //获取role
        String role = (String) data.get("role");
        //获取rid
        int rid = (int) data.get("rid");

        //获取考试科目
        String subject = testPaper.getPaperSubject();
        //获取考试时间
        String time = testPaper.getPaperDate();
        time = "%" + time + "%";
        //获取学期
        int term = testPaper.getPaperTerm().intValue();

        //判断查询什么类型的考试
        Integer type = testPaper.getPaperType();
        int paperId = 0;
        if (type == 4) {
            //月考
            TestPaper monthPaper = testPaperMapper.findMonthPaper(subject, time, term);
            //获取考试的id
            paperId = (int) (monthPaper.getPaperId());
        } else if (type == 5) {
            //期中考
            TestPaper middleTermPaper = testPaperMapper.findMiddleTermPaper(subject, time, term);
            paperId = (int) (middleTermPaper.getPaperId());
        } else if (type == 6) {
            //期末考
            TestPaper endTermPaper = testPaperMapper.findEndTermPaper(subject, time, term);
            paperId = (int) (endTermPaper.getPaperId());
        }

        //用于存放成绩
        List<AnswerSheet> list = new ArrayList<>();
        if ("teacher".equals(role)) {
            //通过rid查询班主任id
            List<Integer> tmids = changeClassMapper.findTmIdByTeacherId(rid);
            if (tmids.size() != 0) {
                //通过tmids查询所管的所有班级
                List<TeamInfo> teamInfos = classMapper.findTeamInfoByTmId(tmids);
                //遍历班级
                for (int i = 0; i < teamInfos.size(); i++) {
                    String tname = teamInfos.get(i).getTname();
                    if (tname.equals(clazz)) {
                        //获取这个班级的所有学生
                        Integer teId = teamInfos.get(i).getTeId();
                        List<StudentInfo> stus = studentMapper.findStusByClassid(teId);
                        for (int j = 0; j < stus.size(); j++) {
                            //遍历学生获取每个学生的成绩
                            int stuid = stus.get(j).getStuid();
                            AnswerSheet sheet = answerSheetMapper.findAnswerSheetByPidAndSid(paperId, stuid);
                            list.add(sheet);
                        }
                    }
                }
            }
        }
        return list;
    }

    /**
     * @Author: 李英勇
     * @Description: 查询学生所有的月考成绩
     * @DateTime: 2020/8/5 16:31
     * @Params: [testPaper, refreshToken, clazz]
     * @Return java.util.Map<java.lang.String, java.util.List < com.woniuxy.commons.entity.AnswerSheet>>
     */
    @Override
    public Map<String, List<AnswerSheet>> findMonthScore(TestPaper testPaper, String refreshToken, String clazz) {
        //在redis中获取用户的role和rid
        //通过refreshToken获取数据
        Map<Object, Object> data = redisTemplate.opsForHash().entries(refreshToken);
        //获取role
        String role = (String) data.get("role");
        //获取rid
        int rid = (int) data.get("rid");

        //获取考试科目
        String subject = testPaper.getPaperSubject();
        //获取考试时间
        String time = testPaper.getPaperDate();
        time = "%" + time + "%";
        //获取学期
        int term = testPaper.getPaperTerm().intValue();

        List<TestPaper> allMonthPapers = testPaperMapper.findAllMonthPapers(subject, time, term);
        List<Integer> list = new ArrayList<>();
        for (int i = 0; i < allMonthPapers.size(); i++) {
            list.add((int) (allMonthPapers.get(i).getPaperId()));
        }

        //用于存放成绩
        Map<String, List<AnswerSheet>> map = new HashMap<>();
        //用于封装查询条件
        Map<String, Object> mapCondition = new HashMap<>();

        if ("teacher".equals(role)) {
            //通过rid查询班主任id
            List<Integer> tmids = changeClassMapper.findTmIdByTeacherId(rid);
            if (tmids.size() != 0) {
                //通过tmids查询所管的所有班级
                List<TeamInfo> teamInfos = classMapper.findTeamInfoByTmId(tmids);
                //遍历班级
                for (int i = 0; i < teamInfos.size(); i++) {
                    String tname = teamInfos.get(i).getTname();
                    if (tname.equals(clazz)) {
                        //获取这个班级的所有学生
                        Integer teId = teamInfos.get(i).getTeId();
                        List<StudentInfo> stus = studentMapper.findStusByClassid(teId);
                        for (int j = 0; j < stus.size(); j++) {
                            //遍历学生获取每个学生的成绩
                            int stuid = stus.get(j).getStuid();
                            mapCondition.put("sid", stuid);
                            mapCondition.put("pid", list);
                            List<AnswerSheet> sheets = answerSheetMapper.findAllAnswerByPidAndSid(mapCondition);
                            map.put(stus.get(j).getSname(), sheets);

                        }
                    }
                }
            }
        }


        return map;
    }
}
